package tfts

import (
	"strings"
)

type heroType int

const (
	Physics heroType = iota
	Magic
)

type modifier struct {
	attrMeta
}

type championMeta struct {
	attrMeta
	Role     Role
	name     string
	dmgType  heroType
	attack   float64
	speed    float64
	initMana int
	Next     *championMeta
	Prev     *championMeta
	hook     Hook
	count    int
	skill    *skill // skill和champion具有相同的成员变量attrMeta,因此不能内嵌
}

func baseModifier() *modifier {
	return &modifier{attrMeta{
		factor:     100,
		ad:         100,
		ap:         100,
		as:         100,
		amp:        100,
		adAmp:      100,
		apAmp:      100,
		atkAmp:     100,
		castAmp:    100,
		critChance: 25,
		critAmp:    40,
		manaAmp:    1.0,
		fighter:    &fighter{},
	}}
}

func defaultChamp() *championMeta {
	attr := attrMeta{
		factor: 100,
	}
	meta := &championMeta{
		attrMeta: attr,
		speed:    0.75,
	}
	return meta
}

func (h *modifier) valid() bool {
	return true
}

// 不复用form以降低复杂度
func (c *championMeta) attach(f *Fight) *Form {
	// 存在形态循环的场景，因此依然需要复制
	cp := *c
	form := &Form{
		id:           c.name,
		championMeta: &cp,
		attached:     f,
		Hook:         c.hook,
		remain:       c.count,
	}
	if form.match(TimeGoE) {
		form.end = f.ticks + form.remain*tick
	}
	form.ApplyRole()
	f.Form = form
	f.attrs = append(f.attrs, form)
	f.handlers = append(f.handlers, form)
	if f.Current() <= 0 {
		form.mana = float64(c.initMana)
	} else {
		f.publish(TransformE)
	}
	return form
}

// 增加形态
func (c *championMeta) Transform(name string, event Event, count int) *championMeta {
	// 创建英雄以生成bundle
	Hero(name)
	key := strings.ToLower(name)
	bundle := bundles[key]
	// 复制以避免干扰
	cp := *bundle.championMeta
	cp.name = name
	cp.Prev = c
	c.Next = &cp
	c.hook = Hook{event: event}
	c.count = count
	return c.Next
}

func (c *championMeta) LoopForm(event Event, count int) *championMeta {
	head := c
	for ; head.Prev != nil; head = head.Prev {
	}
	c.Next = head
	c.hook = Hook{event: event}
	c.count = count
	return c.Next
}
